package com.gzx.plugin.website.user.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzx.plugin.website.user.entity.GzxWebsiteUserLog;
import com.gzx.plugin.website.user.entity.bo.GzxWebsiteUserLogQueryBo;
import com.gzx.plugin.website.user.entity.vo.GzxWebsiteUserLogVo;
import com.gzx.plugin.website.user.mapper.GzxWebsiteUserLogMapper;
import com.gzx.plugin.website.user.service.GzxWebsiteUserLogService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.util.CommonCryptogramUtil;
import vip.xiaonuo.common.util.CommonExcelUtil;

import java.io.IOException;
import java.util.List;

/**
 * @author zjt
 * @description
 * @date 2025/1/8 17:03
 */
@Service
@RequiredArgsConstructor
public class GzxWebsiteUserLogServiceImpl implements GzxWebsiteUserLogService {

    private final GzxWebsiteUserLogMapper websiteUserLogMapper;

    /**
     * 分页查询查询记录列表
     *
     * @param queryBo 查询条件
     * @return 查询记录分页列表
     */
    @Override
    @DS("data")
    public Page<GzxWebsiteUserLogVo> pageList(GzxWebsiteUserLogQueryBo queryBo) {
        return websiteUserLogMapper.selectVoPage(new Page<>(queryBo.getCurrent(), queryBo.getSize()), buildQueryWrapper(queryBo));
    }

    /**
     * 查询符合条件的查询记录列表
     *
     * @param bo 查询条件
     * @return 查询记录列表
     */
    @Override
    @DS("data")
    public void export(GzxWebsiteUserLogQueryBo bo, HttpServletResponse response) throws IOException {
        List<GzxWebsiteUserLogVo> websiteUserLogExcelVoList = websiteUserLogMapper.selectVoList(buildQueryWrapper(bo));
        if (CollectionUtil.isEmpty(websiteUserLogExcelVoList)) {
            throw new CommonException("暂无可导出的数据");
        }
        CommonExcelUtil.exportExcel("官网后台数据", websiteUserLogExcelVoList, GzxWebsiteUserLogVo.class, response);
    }

    @Override
    @DS("data")
    public List<GzxWebsiteUserLog> list(Long id) {
        return websiteUserLogMapper.selectList(new LambdaQueryWrapper<GzxWebsiteUserLog>().gt(GzxWebsiteUserLog::getId, id).last("limit 10000"));
    }

    private LambdaQueryWrapper<GzxWebsiteUserLog> buildQueryWrapper(GzxWebsiteUserLogQueryBo queryBo) {
        LambdaQueryWrapper<GzxWebsiteUserLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(queryBo.getAccuserIdCard()), GzxWebsiteUserLog::getAccuserIdCard, CommonCryptogramUtil.doAesCFBEncrypt(queryBo.getAccuserIdCard()));
        queryWrapper.eq(StringUtils.isNotBlank(queryBo.getMobile()), GzxWebsiteUserLog::getMobile, CommonCryptogramUtil.doAesCFBEncrypt(queryBo.getMobile()));
        queryWrapper.between(GzxWebsiteUserLog::getCreateTime, queryBo.getBeginTime(), queryBo.getEndTime());
        queryWrapper.orderByDesc(GzxWebsiteUserLog::getId);
        return queryWrapper;
    }
}
